<?php

class ToolbaseDatabaseManager {
        
  static protected
    $instance   = null;
  
  protected $lang_databases = array();
  protected $server_databases = array();
  
  protected $mycnf = array();
  
  protected function __construct()
  {  
  }

  /**
   * Retrieves the singleton instance of this class.
   *
   * @param  string $cacheFile  The file path to save the cache
   *
   * @return ToolbaseDatabaseManager
   */
  static public function getInstance()
  {
    if (!isset(self::$instance))
    {
      self::$instance = new self();
    }

    return self::$instance;
  }
  
  public function getConfig( $config_path = null ) {
                
                if( count( $this->mycnf ) ) return $this->mycnf;
                
                if( is_null( $config_path ) ) $config_path = "/home/" . get_current_user() . "/.my.cnf";
                
                $mycnf = parse_ini_file( $config_path );
                
                $this->mycnf = array( 'user' => $mycnf['user'], 'password' => $mycnf['password'] );
                
                return $this->mycnf;
  }
  
  public function getServerFromLang( $lang = 'en', $wiki = 'wikipedia' ) {
        
        $key = sprintf( '%s.%s.org', $lang, $wiki );
        
        if( !isset( $this->lang_databases[$key] ) ) {
                $this->lang_databases[$key] = ToolbaseDatabase::newFromLang( $this, $lang, $wiki );
        }
        
        return $this->lang_databases[$key];
        
  }
  
  public function getServerFromName( $server = 'sql-s1', $database = 'enwiki_p' ) {
        
        $key = sprintf( '%s|%s', $server, $database );
        
        if( !isset( $this->server_databases[$key] ) ) {
                $this->server_databases[$key] = ToolbaseDatabase::newFromServer( $this, $server, $database );
        }
        
        return $this->server_databases[$key];
        
  }
  
  public function getServerFromRequest( sfWebRequest $request ) {
        
        $wiki = $request->getParameter( 'wiki', 'wikipedia' );
        $lang = $request->getParameter( 'lang', 'en' );
                        
        return $this->getServerFromLang( $lang, $wiki );
        
  }
  
  public static function hasInstance() {
        return is_object( self::$instance );
  }
  
  public function getZendDBEvents() {
  
        $events = array();
        
        foreach( array_merge( $this->server_databases, $this->lang_databases ) as $key => $db ) {
                $profiler = $db->getObject()->getProfiler();
                if( $profiler ) {
                        $profiles = $profiler->getQueryProfiles();
                        if( $profiles ) {
                                $events = array_merge( $events, $profiles );
                        }
                }
                
        }
        
        // sequence events
    ksort($events);
        
  }
  
  public function getLangServer() {
        $servers = $this->lang_databases;
        return array_pop( $servers );
  }
  
}
